package com.wyman.core.io;




import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

/**
 * @Author 李伟民
 * @Description 获得类路径的输入流
 * @DateTime 2024/12/6 01:07
 */
public class ClassPathResource implements Resource{

    private final String path;
    private ClassLoader classLoader;

    public ClassPathResource(String path,ClassLoader classLoader){

        this.path = path;
        this.classLoader = classLoader==null?  ClassPathResource.class.getClassLoader() :classLoader;
    }

    @Override
    public InputStream getInputStream() throws IOException {
        InputStream is =  classLoader.getResourceAsStream(path);
        //增加健壮性 考虑路径下没有找到配置文件
        if(is==null){
            throw new FileNotFoundException(this.path+"dont find a configure file ");
        }
        return is;
    }
}
